import { Component, Output, EventEmitter } from '@angular/core';
import { NzIconModule } from 'ng-zorro-antd/icon';
import { NzToolTipModule } from 'ng-zorro-antd/tooltip';

@Component({
  selector: 'app-tabs',
  templateUrl: './tabs.component.html',
  styleUrls: ['./tabs.component.styl']
})

export class TabsComponent {

  @Output() tabClick = new EventEmitter();

	/*
	*{'delTab': tabdata,'curTab': tabdata}
	*/
  @Output() closeTab = new EventEmitter();

  @Output() displayTabSet = new EventEmitter();

  tabs = [{ name: 'home', path: 'home', text: '首页', icon: 'bank' }];

  selectIdx = 0;

  tabsetHidden = false;

  onCloseTab(tab: any): void {
    let optionIndex = this.tabs.indexOf(tab);
    let optionCount = this.tabs.length;
    this.tabs.splice(optionIndex, 1);
    let tabInfo = { 'delTab': tab, 'curTab': null };
    // 正在关闭激活tab的右侧tab
    if (optionCount - 1 > optionIndex) {
      // 关闭前面的tab, 索引下标前移一位
      this.selectIdx = optionIndex;
      tabInfo.curTab = this.tabs[this.selectIdx];
      this.closeTab.emit(tabInfo);
    }
    //关闭时，默认激活左边的
    else if (optionIndex >= 1) {
      this.selectIdx = optionIndex - 1;
      // 激活上一个路由
      tabInfo.curTab = this.tabs[this.selectIdx];
      this.closeTab.emit(tabInfo);
    }
    // 正在关闭激活tab的右侧tab
    else {
    }

  }

  newTab(tabData): void {
    for (let i = 0; i < this.tabs.length; i++) {
      let tab = this.tabs[i];
      if (tab.name === tabData.name) {
        this.selectIdx = i;
        return;
      }
    }
    this.tabs.push(tabData);
    this.selectIdx = this.tabs.length - 1;
  }

  onTabClick(tab: any) {
    this.tabClick.emit(tab);
  }

  onDisplayTab(hidden){
    this.tabsetHidden = hidden;
    this.displayTabSet.emit(hidden);
  }
}
